package com.li.jiang.utils.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel(description = "分页查询实体")
public class PageQuery {

    @ApiModelProperty("页码 默认为第一页")
    private Integer pageNo = 1;
    @ApiModelProperty("页面大小 默认7条数据")
    private Integer pageSize = 7;
    @ApiModelProperty("排序字段")
    private String sortBy;
    @ApiModelProperty("是否升序 true(默认) or false")
    private Boolean isAsc = true;

    public <T> Page<T> toMpPage(OrderItem ... items){
        //1.分页条件
        Page<T> page = Page.of(pageNo,pageSize);
        //2.排序条件

        //1.3非空判断
        if (StrUtil.isNotEmpty(sortBy)){
//            不为空
            if (isAsc) {
                page.addOrder(OrderItem.asc(sortBy));
            }else {
                page.addOrder(OrderItem.desc(sortBy));
            }
        }else if (items != null){
//            为空 ,默认排序
            page.addOrder(items);

        }

    return page;
    }

    public <T> Page<T> toMpPageDefaultByCreateTime(){

        return toMpPage(OrderItem.desc("create_time"));
    }
    public <T> Page<T> toMpPageDefaultByUpdateTime(){

        return toMpPage(OrderItem.desc("update_time"));
    }

    public <T> Page<T> toMpPage(String defaultSortBy , Boolean defaultAsc){

        if (defaultAsc){
            return toMpPage(OrderItem.asc(defaultSortBy));
        }else {
            return toMpPage(OrderItem.desc(defaultSortBy));
        }

    }
}
